package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.R;
import com.gxa.entity.DistrictSysNews;
import com.gxa.mapper.NewsMapper;
import com.gxa.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;
    R r = new R();

    /**
     * 分页查询所有消息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findnews(Integer currentPage, Integer pageSize) {
        Page page = PageHelper.startPage(currentPage,pageSize);
        List<DistrictSysNews> newsList = newsMapper.findnews();
        int total = (int)page.getTotal();
        Map map = new HashMap();
        map.put("total",total);
        map.put("newsList",newsList);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);
        return r;
    }

    /**
     * 根据消息类型，创建日期，消息状态查询消息
     * @param currentPage
     * @param pageSize
     * @param newsType
     * @param createDate
     * @param newsStatus
     * @return
     */
    @Override
    public R findnewsbyconditions(Integer currentPage, Integer pageSize, String newsType, Date createDate, Integer newsStatus) {
        Page page = PageHelper.startPage(currentPage,pageSize);
        List<DistrictSysNews> newsList = newsMapper.findnewsbyconditions(newsType,createDate,newsStatus);
        int total = (int)page.getTotal();
        Map map = new HashMap();
        map.put("total",total);
        map.put("newsList",newsList);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);
        return r;
    }

    /**
     * 所有消息全部标记为已读
     * @return
     */
    @Override
    @Transactional
    public R allmarkasread() {
        ArrayList newsIdList = newsMapper.findUnReadNews();
        newsMapper.allmarkasread(newsIdList);
        r.setCode(200);
        r.setMsg("标记成功");
        return r;
    }
}
